import { observable, action, toJS } from 'mobx';
import { Modal, Tools, Bubble } from 'weaver-mobile'
import { ListPage, ShareAuth } from 'weaver-mobile-page';
import * as Api from '../../api/CommunicationApi';
const alert = Modal.alert;
const getLabel = Tools.getLabel;
export default class ParterListStore {
  @observable coworkId = '';
  @observable parterlistLoading = true;
  @observable parterlist = null;
  @observable shareConditionInfo = []; // 参与范围Condition
  @observable shareAuthRoute = null;
  @observable params = {
    id: '',
    shareType: '', // 参与类型--1：人员；2：分部；3：部门；4：角色；5：所有人；6:岗位
    shareValue: '', // 参与内容
    secLevel: '', // 安全等级
    jobtitleLevel: '', // 岗位等级（0：总部；1：部门；2：分部）
    jobtitleScopeid: '', // 对应jobtitlelevel所选择的，取其对应的安全级别。
    secLevelMax: '', // 安全等级最大
    shareId: '', // 删除传参与人ID串
	};
	@observable currentModel = {}; //当前对话框弹窗

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  inital = (path, params) => {
    params = {
      coworkId: this.coworkId,
      ...params
    }
    this.parterlist = new ListPage.Store({ route: path, id:'parterList' });
    this.shareAuthRoute = new ShareAuth.Store({ route: path, id: 'addShare' });
    this.getParterListData(params);
    this.getWeaAuthConditions();
  }

  @action('参加范围Condition请求')
  getWeaAuthConditions = () => {
    Api.getWeaAuthConditions().then(res => {
      this.shareConditionInfo = res.conditioninfo;    
    });
  }

  @action('添加参与范围--展示人员列表')
  getParterListData = (params) => {
    this.parterlistLoading = true;
    Api.getParterListData(params).then((res) => {
      const {status, shareList} = res;
      if(status === "1"){
        this.parterlist.setState({data: shareList})
        this.parterlistLoading = false;
      }
    });
  }

  @action('保存参与人')
  parterListChange = (data, callback) => {
    const { has_child, secLevel, secLevelMax, shareType, shareValue, shareValuespan, roleLevel, jobtitleLevel, typeId } = data;
    let params = {
      id: this.coworkId,
      shareType, // 参与类型--1：人员；2：分部；3：部门；4：角色；5：所有人；6:岗位
      typeId, // 版块
      // shareValue, // 都有{所有人没有}，，人力资源，分部，参与内容
      // shareValuespan,
      // has_child: has_child || 0, // 分部,部门
      // secLevel, // 分部,部门，角色，所有人--安全等级
      // secLevelMax, // 分部,部门，角色，所有人--安全等级最大
      // jobtitleLevel: toJS(jobtitleLevel.jobtitleLevel), // 岗位-- 岗位等级（0：总部；1：部门；2：分部）
      // jobtitleScopeid: toJS(jobtitleLevel.jobtitleScopeid), // 岗位--对应jobtitlelevel所选择的，取其对应的安全级别。
      // roleLevel, // 角色
    }
    if (shareType === '2' || shareType === '3') {
      params = {...params, shareValue, shareValuespan, has_child: has_child || 0, secLevel, secLevelMax}
    } else if (shareType === '4') {
      params = {...params, shareValue, shareValuespan, secLevel, secLevelMax, roleLevel}
    } else if (shareType === '5') {
      params = {...params, secLevel, secLevelMax}
    } else if (shareType === '6') {
      params = {...params, shareValue, shareValuespan, jobtitleLevel: toJS(jobtitleLevel.jobtitleLevel), jobtitleScopeid: toJS(jobtitleLevel.jobtitleScopeid),}
    } else if (shareType === '1') {
      params = {...params, shareValue, shareValuespan}
    }

    Api.saveorupdateCoworkShare(params).then(res => {
      if(res.flag) {
        this.getParterListData({coworkId: this.coworkId});
        if (callback && typeof callback === 'function') callback();
      }
    })
  }

  @action('参与范围--删除操作')
  parterDelete = ({coworkId, id, typeId}, callback) => {
    this.currentModel = alert(getLabel(388758, '确认要删除吗？'),'', [
      { text: getLabel(31129, '取消') },
      { text: getLabel(33703, '确认'),
        onPress: () => {
          let params = {
            id: coworkId,
            shareId: id,
            typeId,
          }
          Api.saveorupdateCoworkShare(params).then(res => {
            if (res.status === "1") {
              this.getParterListData({coworkId: this.coworkId});
              Bubble.success(getLabel(30700, '操作成功')+'!', 1);
              if (callback && typeof callback === 'function') callback();
            }
          })
        }
      },
    ])
  }

  clear = () => {
    this.shareConditionInfo = [];
	}
	
	//关闭所有弹窗
	@action closeAllModel = () => {
		this.currentModel && typeof this.currentModel.close === 'function' && this.currentModel.close();
	}
}